<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/26  14:05
 */
namespace app\admin\controller;
class Schedule extends Common
{
    private $table_admin;

    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('project_schedule');
    }

    //列表
    public function lists()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $keyword = input('keyword');
        if( $keyword != ''){
            $where['name'] = array('like','%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
            $this->assign('keyword', $keyword);
        }

        $result = $this->table_admin
            ->where($where)
            ->order('id desc')
            ->paginate(15, false, $pageParam);
//        halt($article_result);
        $page  = $result->render();
        $this->assign('page', $page);
        $this->assign('result', $result);

        return $this->fetch();
    }

    //添加
    public function add()
    {
        return $this->fetch('Schedule/edit');
    }

    //编辑
    public function edit()
    {
        if(request()->isPost()){
            $id = input('id');
            $name = input('name');
            $describe = input('describe');
            $username = input('username');
            $schedule = input('schedule');
            $introduction = input('introduction');
            $signtime = input('signtime');
            $finishtime = input('finishtime');

            $data['name'] = $name;
            $data['describe'] = $describe;
            $data['username'] = $username;
            $data['schedule'] = $schedule;
            $data['introduction'] = $introduction;
            $data['signtime'] = strtotime($signtime);
            $data['finishtime'] = strtotime($finishtime);

            if(!$id){
                //添加
                $bool = $this->table_admin->insert($data);
            }else{
                //修改
                $bool = $this->table_admin->where(['id'=>$id])->update($data);
            }

            if($bool){
                $this->success('操作成功', url('Schedule/lists'));
            }else{
                $this->error('操作失败');
            }

        }
        $id = input('id');
        $this->assign('id', $id);
        $data = $this->table_admin->where(['id'=>$id])->find();
        $data['signtime'] = date('Y-m-d H:i:s', $data['signtime']);
        $data['finishtime'] = date('Y-m-d H:i:s', $data['finishtime']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function info()
    {
        $id = input('id');
        $title = input('title');
        $this->assign('title', $title);
        $type = [
            1=>'情况描述',
            2=>'进度安排',
            3=>'项目说明',
        ];
        $content = [
            1=>'describe',
            2=>'schedule',
            3=>'introduction',
        ];
        $this->assign('content', $content);
//        $data['describe']=$data[$content[$title]];
        $this->assign('type', $type);
        $data = $this->table_admin->where(['id'=>$id])->find();
        $data['title'] = $title;
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function delete()
    {
        $id = input("id");
        if(!$id) $this->error('参数有误');
        $da=$this->table_admin->where("id='$id'")->find();
        if(!$da) $this->error('数据不存在');
        $res=$this->table_admin->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('Project/index'));

    }
}
